package com.hw.one.collect.util;


import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class ByteUtil {
	/**
	 * hex字符串转换为byte[]
	 * @param hex
	 * @return
	 */
	public static byte[] hex2byte(String hex) {
		byte[] bts = new byte[hex.length() / 2];
		for (int i = 0, j = 0; j < bts.length; j++) {
			bts[j] = (byte) Integer.parseInt(hex.substring(i, i + 2), 16);
			i += 2;
		}
		return bts;
	}
	/**
	 * byte[]转换为hex字符串,从低位开始
	 * @param b
	 * @return
	 */
	public static String byte2hex(byte[] b) {
		if(b==null)return null;
		StringBuffer buf = new StringBuffer(b.length * 2);
		for (int i = 0; i < b.length; i++) {
			String hex = Integer.toHexString(b[i] & 0xFF);
			if (hex.length() == 1)
				buf.append('0');
			buf.append(hex.toUpperCase());
		}
		return buf.toString();
	}




	/**
	 * byte[]转换为hex字符串,从高位开始
	 * @param
	 * @return
	 */
	public static String byte2hex1(byte[] msg){
		StringBuffer result = new StringBuffer();
		for(int i = msg.length-1;i>=0;i--){
			byte b = msg[i];
			int a = b & 0xff;
			if(a<0x10){
				result.append(0+Integer.toHexString(a)+"");
			}else{
				result.append(Integer.toHexString(a)+"");
			}
		}
		return result.toString().toUpperCase();
	}





	public static int hilowdata(byte highByte, byte lowByte) {
		return (highByte & 0xff) << 8 | (lowByte & 0xff);
	}

	public static byte[] reverse(byte[] datas) {
		int len = datas.length;
		byte[] rs = new byte[len];
		for(int i=0; i < len;i++) {
			rs[i] = datas[len - i - 1];
		}
		return rs;
	}
	public static int hilowdata(byte[] vs) {
		int[] ints = new int[vs.length];
		for (int i = 0; i < vs.length; i++) {
			ints[i] = (vs[i] & 0xff) << i * 8;
		}
		int value = ints[0];
		for (int a = 1; a < ints.length; a++) {
			value = value | ints[a];
		}
		return value;
	}

	public static byte[] arraycopy(byte[] src, int offset, int len) {
		byte[] buf = new byte[len];
		System.arraycopy(src, offset, buf, 0, len);
		return buf;
	}

	public static String encodeBase64(byte[] input) throws Exception {
		return new sun.misc.BASE64Encoder().encode(input);
	}

	public static byte[] decodeBase64(String input) throws Exception {
		byte[] bt = null;
		try {
			sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
			bt = decoder.decodeBuffer(input);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bt;
	}



	public  static byte[] toArray(List<Byte> bytes) {
		byte[] byteArr = new byte[bytes.size()];
		for(int i =0; i < bytes.size();i++) {
			byteArr[i] = bytes.get(i);
		}
		return byteArr;
	}

	public static final byte[] str2byte(String s){
		return s.getBytes();
	}


	/**
	 * @param len bytes长度
	 * @return 长度 占四个字节
	 */
	public static final byte[] getBytesLength(int len){
		byte[] targets = new byte[4];
		targets[3] = (byte) (len & 0xFF);
		targets[2] = (byte) (len >> 8 & 0xFF);
		targets[1] = (byte) (len >> 16 & 0xFF);
		targets[0] = (byte) (len >> 24 & 0xFF);
		return targets;
	}

	public static final int[] byteArr2intArr(byte[] bytes){
		int[] intArr = new int[bytes.length/2];
		for (int i = 0, k = 0; i < bytes.length; i++) {
			if(i % 2 == 1){
				intArr[k] = ((bytes[i] & 0xff) | ((bytes[i-1] & 0xff) << 8));
				k++;
			}
		}
		return intArr;
	}

//	public static void main(String[] args){
//	    byte[] b = {0x07, (byte)0xdf};
//		int[] ints = byteArr2intArr(b);
//		System.out.println(Arrays.toString(ints));
//	}


	public static byte[] hexStringToBytes(String hexString) {
		if (hexString == null || hexString.equals("")) {
			return null;
		}
		hexString = hexString.toUpperCase();
		int length = hexString.length() / 2;
		char[] hexChars = hexString.toCharArray();
		byte[] d = new byte[length];
		for (int i = 0; i < length; i++) {
			int pos = i * 2;
			d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		}
		return d;
	}
	/**
	 * Convert char to byte
	 * @param c char
	 * @return byte
	 */
	private static byte charToByte(char c) {
		return (byte) "0123456789ABCDEF".indexOf(c);
	}


}
